package br.edu.faculdadeprojecao.si.sisecon.action;

import java.io.Serializable;

import javax.enterprise.context.Conversation;
import javax.faces.application.FacesMessage;
import javax.faces.application.FacesMessage.Severity;
import javax.faces.context.FacesContext;
import javax.inject.Inject;

public abstract class AbstractAction implements Serializable {
	
	private static final long serialVersionUID = 1L;
	
	@Inject
	protected Conversation conversation;
	
	protected void beginConversation() {
        if (conversation.isTransient()) {
            conversation.begin();
        }
    }

	protected void endConversation() {
        if (!conversation.isTransient()) {
            conversation.end();
        }
    }
	
	protected FacesContext getFacesContext() {
		return FacesContext.getCurrentInstance();
	}
	
	protected void addMessageInfo(String message) {
		addMessage(FacesMessage.SEVERITY_INFO, message);
	}
	
	protected void addMessageWarn(String message) {
		addMessage(FacesMessage.SEVERITY_WARN, message);
	}
	
	protected void addMessageError(String message) {
		addMessage(FacesMessage.SEVERITY_ERROR, message);
	}
	
	protected void addMessage(Severity severity, String message) {
		FacesContext context = getFacesContext();
		context.addMessage(null, new FacesMessage(severity, message, ""));
	}

}
